<?php

	class AppCache_APC extends AppCache implements Interface_AppCache
	{

		public function connect()
		{
			return true;
		}

		public function set($key, $value, $timeout = NULL, $check_key = true)
		{
			if (!$this->enabled) return true;

			$timeout = (int)(is_null($timeout) ? $this->timeout : $timeout);

			if (apc_add($this->getPrefix() . $key, $value, $timeout)) {
				$this->datas[$key] = $value;

				return true;
			} else {
				return false;
			}

		}

		public function sets($values, $timeout = NULL, $check_key = true)
		{
			if (!$this->enabled) return true;

			$result = true;
			foreach ($values as $key => $value)
				$result &= $this->set($key, $value, $timeout);

			return $result;
		}

		public function get($key, $check_key = true)
		{
			if (!$this->enabled) return null;

			if (isset($this->datas[$key])) {
				return $this->datas[$key];
			}

			$success = false;
			$value   = apc_fetch($this->getPrefix() . $key, $success);

			return $success ? $value : null;
		}

		public function gets($keys, $check_key = true)
		{
			if (!$this->enabled) return null;

			$result = true;
			foreach ($keys as $key)
				$result &= $this->get($key);

			return $result;
		}

		public function del($key)
		{
			if (!$this->enabled) return true;

			return apc_delete($this->getPrefix() . $key);
		}

		public function increment($key, $value)
		{
			if (!$this->enabled) return null;

			$success = false;
			$result  = apc_inc($this->getPrefix() . $key, $value, $success);

			if (!$success) {
				return $this->set($this->getPrefix() . $key, $value) ? $value : null;
			} else {
				$this->datas[$key] = $result;

				return $result;
			}


		}

	}

